﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace CommonProject_DeskTop.ViewModel
{
    public class MainViewModel : ObservableObject
    {
        List<object> views;

        #region 属性
        [ObservableProperty]
        private string dateTimeStr;
        public string DateTimeStr
        {
            get { return dateTimeStr; }
            set { SetProperty(ref dateTimeStr, value); }
        }

        private object bodyView;
        public object BodyView
        {
            get { return bodyView; }
            set { SetProperty(ref bodyView, value); }
        }



        #endregion

        #region 命令
        public RelayCommand<string> ButtonClickCmd { get; set; }
        public RelayCommand<object>MenuClickCmd { get; set; }

        #endregion

        #region 方法

        private void RegisterMessage()
        {
            ////注册一个系统菜单功能消息
            //WeakReferenceMessenger.Default.Register<Type>(this, new Action<string, Type>((msg, type) => { BodyViewChanged(msg, type); }));
        }

        #region 系统按钮方法
        private void FunMenu(string p)
        {
            switch (p)
            {
                case "最小化":
                    WindowMin();
                    break;
                case "最大化":
                    WindowMax();
                    break;
                case "关闭":
                    AppClose();
                    break;
            }
        }
        private void AppClose()
        {
            WeakReferenceMessenger.Default.Send("MainWindowClose");
        }

        private void WindowMax()
        {
            WeakReferenceMessenger.Default.Send("MainWindowMax");
        }

        private void WindowMin()
        {
            WeakReferenceMessenger.Default.Send("MainWindowMin");
        }
        #endregion

        #region 菜单按钮方法
        private void MenuChange(object obj)
        {
            var menu =obj as ListBoxItem;
            switch (menu.Content.ToString())
            {
                case "首页":
                    //MessageBox.Show(menu.Content.ToString(), "");
                    BodyViewChanged(this,typeof(View.HomeUI));
                    break;
                case "物资管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "仓库设置":
                    //MessageBox.Show(menu.Content.ToString(),"");
                    BodyViewChanged(this, typeof(View.InStoreManager));
                    break;
                case "库位管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "客户管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "供应商管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "物资入库":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "物资出库":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "入库管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
                case "出库管理":
                    MessageBox.Show(menu.Content.ToString(), "");
                    break;
            }
        }

        /// <summary>
        /// 主界面视图切换
        /// </summary>
        void BodyViewChanged(object recipient,Type view)
        {
            var currentView = views.FirstOrDefault(p => p.GetType() == view);
            if (currentView == null)
            {
                BodyView = Activator.CreateInstance(view) as UserControl;
                views.Clear();//清理旧页面，每次保证页面重新加载，不清理则可以使用缓存，提高软件流畅度
                views.Add(BodyView);
            }
            else
            {
                BodyView = currentView;
            }
        }

        #endregion

        private void CreateTimer()
        {
            #region 系统时间定时器获取时间数据
            DispatcherTimer cpuTimer = new DispatcherTimer
            {
                Interval = new TimeSpan(0, 0, 0, 1, 0)
            };
            cpuTimer.Tick += DispatcherTimer_Tick;
            cpuTimer.Start();
            #endregion
        }

        private void DispatcherTimer_Tick(object sender, EventArgs e)
        {
            DateTimeStr = DateTime.Now.ToString("yyyy年MM月dd日 HH:mm:ss");
        }


        #endregion

        public MainViewModel()
        {

            BodyView = new View.HomeUI();
            views = new List<object>();
            views.Add(BodyView);

            ButtonClickCmd = new RelayCommand<string>(FunMenu);
            MenuClickCmd = new RelayCommand<object>(MenuChange);
            //创建定时器
            CreateTimer();
            //消息注册
            RegisterMessage();
        }

       
    }
}
